package com.dwxt.community.controller;

import com.dwxt.common.annotation.*;
import com.dwxt.common.base.*;
import com.dwxt.common.util.*;
import com.dwxt.community.dao.UserMapper;
import com.dwxt.community.entity.*;
import com.dwxt.community.service.*;
import com.dwxt.community.service.feign.*;
import com.github.pagehelper.*;
import org.apache.commons.lang.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RequestMapping("/cms/recycle")
@RestController
public class RecycleController {

    @Autowired
    private RecycleService recycleService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private SmsService smsService;
    @Autowired
    private RecoveryOndoorService recoveryOndoorService;
    @Autowired
    private CommunityService communityService;
    @Autowired
    private UserMapper userMapper;

    /**
     * showdoc
     *
     * @param id        可选 int 主键(新增)
     * @param phone     必选 string 电话
     * @param address   必选 string 详细地址
     * @param username  必选 string 用户名
     * @param isDefault 可选 int 是否是默认回收地址(1:是，0:否)
     * @return {"status":200,"msg":"SUCCESS","data":null}
     * @catalog 社区/回收地址
     * @title 创建或修改回收地址
     * @description 创建或修改回收地址
     * @url /cms/recycle/createOrUpdateAddress
     */
    @RequestMapping("/createOrUpdateAddress")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, filedName = "phone", regStr = "^1[3|4|5|6|7|8|9][0-9]\\d{4,8}$"),
            @ValidateFiled(index = 0, filedName = "address", notNull = true),
            @ValidateFiled(index = 0, filedName = "username", notNull = true),
            //@ValidateFiled(index = 0, filedName = "isDefault", notNull = true),
    })
    public BaseResult createOrUpdateAddress(RecycleAddress recycleAddress) {
        recycleService.createOrUpdateAddress(recycleAddress);
        return BaseResult.ok();
    }

    /**
     * showdoc
     *
     * @return {"status":200,"msg":"SUCCESS","data":{"id":1,"userId":"158502014539731","address":"聚金万佳苑","username":"燕龙","phone":"15123058497","createTime":1585135468000,"isDefault":0}}
     * @catalog 社区/回收地址
     * @title 查询默认回收地址
     * @description 查询默认回收地址
     * @url /cms/recycle/selectDefaultAddress
     */
    @RequestMapping("/selectDefaultAddress")
    public BaseResult selectDefaultAddress() {
        RecycleAddress address = recycleService.selectDefaultAddress();
        return BaseResult.ok(address);
    }

    /**
     * showdoc
     *
     * @param id 必选 int 主键id
     * @return {"status":200,"msg":"SUCCESS","data":{"id":1,"userId":"158502014539731","address":"聚金万佳苑","username":"燕龙","phone":"15123058497","createTime":1585135468000,"isDefault":0}}
     * @catalog 社区/回收地址
     * @title 查询特定的回收地址
     * @description 查询特定的回收地址
     * @url /cms/recycle/selectByKey
     */
    @RequestMapping("/selectByKey")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, notNull = true),
            //@ValidateFiled(index = 0, filedName = "isDefault", notNull = true)
    })
    public BaseResult selectByKey(Integer id) {
        RecycleAddress address = recycleService.selectByKey(id);
        return BaseResult.ok(address);
    }

    /**
     * showdoc
     *
     * @param id 必选 int 主键id
     * @return {"status":200,"msg":"SUCCESS","data":null}
     * @catalog 社区/回收地址
     * @title 删除地址
     * @description 删除地址
     * @url /cms/recycle/deleteAddress
     */
    @RequestMapping("/deleteAddress")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, notNull = true),
    })
    public BaseResult deleteAddress(Integer id){
        recycleService.deleteAddressByKey(id);
        return BaseResult.ok();
    }

    /**
     * showdoc
     *
     * @return {"status":200,"msg":"SUCCESS","data":[{"id":1,"userId":"158502014539731","address":"聚金万佳苑","username":"燕龙","phone":"15123058497","createTime":1585135468000,"isDefault":0}]}
     * @catalog 社区/回收地址
     * @title 查询所有回收地址
     * @description 查询所有回收地址
     * @url /cms/recycle/selectAddressList
     */
    @RequestMapping("/selectAddressList")
    public BaseResult selectAddressList() {
        List<RecycleAddress> recycleAddressList = recycleService.selectAddressList();
        return BaseResult.ok(recycleAddressList);
    }


    /**
     * showdoc
     * @catalog 社区/上门回收
     * @title 上门回收信息发送
     * @description 创建上门回收信息并发送短信给对应人员
     * @param id 可选 int  主键;
     * @param recycleAddressId 必选 int 上门地址回收id;
     * @param weight 必选 int 垃圾重量;
     * @param appointTime 必选 date 上门时间;
     * @param note 必选 string 备注;
     * @param imgUrl 必选 string 垃圾图片;
     * @param isCompleted 可选 int 是否完成;
     * @param userId 必选 string 用户id;
     * @param communityId 必选 int 社区id;
     * @param userName 必选 string 用户名;
     * @param communityName 必选 string 社区名;
     * @url /cms/recycle/createRecycleRecord
     * @return {"status":200,"msg":"SUCCESS","data":null}
     */
    @RequestMapping("/createRecycleRecord")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, filedName = "recycleAddressId", notNull = true),
            @ValidateFiled(index = 0, filedName = "weight", notNull = true),
            @ValidateFiled(index = 0, filedName = "appointTime", notNull = true),
            @ValidateFiled(index = 0, filedName = "communityId", notNull = true),
    })
    public BaseResult createRecycleRecord(RecycleRecord recycleRecord){
        String currentUserId = PartyUtil.getCurrentUserId();
        BaseResult baseResult = redisService.get(currentUserId + "ofRecycleRecord");
        if (baseResult.getData()!=null && baseResult.getData().equals("1")){
            return BaseResult.build(StatusCode.RECYCLE_IS_CREATE_TODAY);
        }else {
            redisService.set(currentUserId+"ofRecycleRecord","1", (long) 86400);
            String username = userMapper.selectByPrimaryKey(currentUserId).getUsername();
            if (StringUtils.isNotEmpty(username)){
                recycleRecord.setUsername(username);
            }
            recycleService.createRecycleRecord(recycleRecord);
            smsService.sendSms(recycleRecord);
            return BaseResult.ok();
        }
    }

    /**
     * showdoc
     * @catalog 社区/查询上门回收人员信息
     * @title 查询上门回收人员信息
     * @description 查询上门回收人员信息
     * @param pageNum  必选 int 当前页
     * @param pageSize 必选 int 最大展示条数
     * @param communityName 可选 string 社区名
     * @url /cms/recycle/selectRecycleOndoorPersonList
     * @return {}
     */
    @RequestMapping("/selectRecycleOndoorPersonList")
    public BaseResult selectRecycleOndoorPersonList(Integer pageNum, Integer pageSize,
                                                    @RequestParam(required = false, value = "communityName") String communityName
    ) {
        PageHelper.startPage(pageNum, pageSize);
        if (StringUtils.isNotEmpty(communityName)) {
            List<RecoveryOndoor> recoveryOndoors = recoveryOndoorService.selecPersontListByName(communityName);
            if (recoveryOndoors != null) {
                PageInfo<RecoveryOndoor> pageInfo = new PageInfo<>(recoveryOndoors);
                return BaseResult.build(StatusCode.SUCCESS, pageInfo);
            } else {
                return BaseResult.build(StatusCode.FAIL);
            }
        } else {
            List<RecoveryOndoor> recoveryOndoors = recoveryOndoorService.selecPersontList();
            if (recoveryOndoors != null) {
                PageInfo<RecoveryOndoor> pageInfo = new PageInfo<>(recoveryOndoors);
                return BaseResult.build(StatusCode.SUCCESS, pageInfo);
            } else {
                return BaseResult.build(StatusCode.FAIL);
            }
        }
    }

    /**
     * showdoc
     * @catalog 社区/查询上门回收记录
     * @title 查询上门回收记录
     * @description 查询上门回收记录
     * @param pageNum 必选 int 当前页码
     * @param pageSize 必选 int 最大展示条数
     * @param userName 可选 string 用户名
     * @param communityName 可选 string 社区名
     * @param time 可选 string 上门回收时间
     * @url /cms/recycle/selectRecycleOndoorList
     * @return {}
     */
    @RequestMapping("/selectRecycleOndoorList")
    public BaseResult selectRecycleOndoorList(Integer pageNum, Integer pageSize,
                                              @RequestParam(required = false, value = "userName") String userName,
                                              @RequestParam(required = false, value = "communityName") String communityName,
                                              @RequestParam(required = false, value = "time") String time
    ) {
        PageHelper.startPage(pageNum, pageSize);
        if (StringUtils.isNotEmpty(userName) | StringUtils.isNotEmpty(communityName) || time != null) {
            List<RecycleRecord> recycleRecords = recycleService.selectByQuery(userName, communityName, time);
            if (recycleRecords.size() > 0) {
                PageInfo<RecycleRecord> pageInfo = new PageInfo<>(recycleRecords);
                return BaseResult.build(StatusCode.SUCCESS, pageInfo);
            } else {
                return BaseResult.build(StatusCode.FAIL);
            }
        } else {
            List<RecycleRecord> recycleRecords = recycleService.selectList();
            if (recycleRecords.size() > 0) {
                PageInfo<RecycleRecord> pageInfo = new PageInfo<>(recycleRecords);
                return BaseResult.build(StatusCode.SUCCESS, pageInfo);
            } else {
                return BaseResult.build(StatusCode.FAIL);
            }
        }
    }


    /**
     * showdoc
     * @catalog 社区/查询社区列表
     * @title 查询社区列表
     * @description 查询社区列表
     * @url /cms/recycle/selectCommunityList
     * @return
     */
    @GetMapping("/selectCommunityList")
    public BaseResult selectCommunityList(){
       return BaseResult.build(StatusCode.SUCCESS,communityService.selectList());
    }

    /**
     *
     * 新增、修改社区信息和上门回收人员
     */
    @PostMapping("/createOrUpdateRecycleOndoorPerson")
    public BaseResult createOrUpdateRecycleOndoorPerson(RecoveryOndoor recoveryOndoor) {
        if (recoveryOndoor.getId()!=null) {
            //更新
            Integer record = recoveryOndoorService.updateRecord(recoveryOndoor);
            if (record > 0) {
                return BaseResult.ok();
            } else {
                return BaseResult.build(StatusCode.FAIL);
            }
        } else {
            List<RecoveryOndoor> recoveryOndoors = recoveryOndoorService.selecPersontListByName(recoveryOndoor.getCommunityName());
            if (recoveryOndoors.size()>0){
                return BaseResult.build(StatusCode.COMMUNITY_IS_EXIST);
            }
            //新增
            Integer record = recoveryOndoorService.createRecord(recoveryOndoor);
            if (record > 0) {
                return BaseResult.ok();
            } else {
                return BaseResult.build(StatusCode.FAIL);
            }
        }

    }


    /**
     * 根据id查看回收信息详情
     * @param id
     * @return
     */
    @RequestMapping("/selectRecycleOndoorPersonById")
    public BaseResult selectRecycleOndoorPersonById(@RequestParam(required = true) Integer id) {
        return BaseResult.build(StatusCode.SUCCESS, recoveryOndoorService.selectById(id));
    }

    /**
     * 删除上门回收人员
     * @param id
     * @return
     */
    @RequestMapping("/deleteRecycleOndoorPerson")
    public BaseResult deleteRecycleOndoorPerson(@RequestParam(required = true) Integer id) {
        Integer record = recoveryOndoorService.deleteRecord(id);
        if (record > 0) {
            return BaseResult.ok();
        } else {
            return BaseResult.build(StatusCode.FAIL);
        }
    }


}
